<template>
	<view >
		<view class="product-window" :class="(attr.cartAttr === true ? 'on' : '')">
			<view class="textpic acea-row row-between-wrapper">
				<view class="pictrue" @click="showImg()">
					<image :src="attr.productSelect.image"></image>
				</view>
				<view class="text">
					<view class="line1">
						{{ attr.productSelect.storeName }}
					</view>
					<view class="money font-color">
						<view class="acea-row row-middle">
							￥<text class="num">{{ attr.productSelect.price }}</text>
							<text class='vip-money'
								v-if="is_vip>0 && attr.productSelect.vipPrice">￥{{attr.productSelect.vipPrice}}</text>
							<view class="vipImg" v-if="is_vip>0 && attr.productSelect.vipPrice">
								<image src="../../static/images/svip.gif"></image>
							</view>
						</view>
						<text class="stock" v-if='isShow'>库存: {{ attr.productSelect.stock }}</text>
						<text class='stock' v-if="limitNum">{{type ? '库存' : "限量"}}: {{attr.productSelect.quota}}</text>
					</view>
				</view>
				<view class="iconfont iconshibai" @click="closeAttr"></view>
			</view>
			<view class="rollTop">
				<view class="productWinList">
					<view v-if="isDate" class="item" style="margin-bottom: 20px;">
						<view class="title">使用日期 </view>
						<view v-if="salesDates&&salesDates.length" class="listn acea-row row-middle">
							<view @click="selectDay(salesDates[0])" v-if="salesDates[0]" class="itemn2" :class="salesDates[0].selected === true ? 'on' : ''">
								<view>{{salesDates[0].week}}</view>
								<view>{{salesDates[0].date_simple}}</view>
							</view>
							<view  @click="selectDay(salesDates[1])" v-if="salesDates[1]" class="itemn2" :class="salesDates[1].selected === true ? 'on' : ''">
								<view>{{salesDates[1].week}}</view>
								<view>{{salesDates[1].date_simple}}</view>
							</view>
							<view  @click="selectDay(selectDayBean)" v-if="selectDayBean.date_simple&&selectDayBean.date!=salesDates[0].date&&selectDayBean.date!=salesDates[1].date&&selectDayBean.date!=salesDates[2].date" class="itemn2" :class="selectDayBean.selected === true ? 'on' : ''">
								<view>{{selectDayBean.week}}</view>
								<view>{{selectDayBean.date_simple}}</view>
							</view>
							<view @click="selectDay(salesDates[2])"  v-else-if="salesDates[2]"  class="itemn2" :class="salesDates[2].selected === true ? 'on' : ''">
								<view>{{salesDates[2].week}}</view>
								<view>{{salesDates[2].date_simple}}</view>
							</view>
							<view @click="calendarShow=true"  v-if="salesDates&&salesDates.length>=3" class="more_date row-center" >
								<view class="">
									<view>更多</view>
									<view>日期</view>
								</view>
								<view class="iconfont iconxiayiyeqianjinchakangengduo "></view>
							</view>
						</view>
						<view v-else style="text-align: center;color: #fa2508;">
							暂无售卖日期
						</view>
					</view>
					<view class="item" v-for="(item, indexw) in attr.productAttr" :key="indexw">
						<view class="title">{{ item.attrName }}</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="item.index === itemn.attr ? 'on' : ''"
								v-for="(itemn, indexn) in item.attrValues" @click="tapAttr(indexw, indexn)"
								:key="indexn">
								{{ itemn }}
							</view>
						</view>
					</view>
					
				</view>
				
				<view class="cart acea-row row-between-wrapper">
					<view class="title">数量</view>
					<view class="carnum acea-row row-left">
						<view class="item reduce acea-row row-center-wrapper"
							:class="attr.productSelect.cartNum <= 1 ? 'on' : ''" v-if="attr.productSelect.cart_num <= 1">
							<text class="iconfont iconjian1"></text>
						</view>
						<view class="item reduce acea-row row-center-wrapper"
							:class="attr.productSelect.cartNum <= 1 ? 'on' : ''" @click="CartNumDes" v-else>
							<text class="iconfont iconjian1"></text>
						</view>
						<view class='item num acea-row row-middle'>
							<input type="number" v-model="attr.productSelect.cartNum"
								data-name="productSelect.cartNum"
								@input="bindCode(attr.productSelect.cartNum)"></input>
						</view>
						<view v-if="iSplus" class="item plus acea-row row-center-wrapper" :class="
				      attr.productSelect.cartNum >= attr.productSelect.stock
				        ? 'on'
				        : ''
				    " @click="CartNumAdd">
							<text class="iconfont iconjia1"></text>
						</view>
						<view v-else class='item plus'
							:class='(attr.productSelect.cartNum >= attr.productSelect.quota) || (attr.productSelect.cartNum >= attr.productSelect.productStock) || (attr.productSelect.cartNum >= attr.productSelect.num) || (type=="seckill" && attr.productSelect.cartNum >= attr.productSelect.onceNum)? "on":""'
							@click='CartNumAdd'>+</view>
					</view>
				</view>
				
			</view>
			<view class="joinBnt bg-color" v-if="attr.productSelect.stock&&(isDate&&selectDayBean.date!='')" @click="selectAttr">确认选择</view>
			<view class="joinBnt bg-color" v-if="attr.productSelect.stock&&!isDate" @click="selectAttr">确认选择</view>
			<view class="joinBnt on" v-if="!attr.productSelect.stock&&!isDate">已售罄</view>
			<view class="joinBnt on" v-if="!attr.productSelect.stock&&isDate">已售罄</view>	
			<view class="joinBnt on" v-else-if="isDate&&selectDayBean.date==''">请选择日期</view>
		</view>
		<view class="mask" @touchmove.prevent :hidden="attr.cartAttr === false" @click="closeAttr"></view>
		<u-popup mode="bottom" v-model="calendarShow" :safe-area-inset-bottom="true"  border-radius="20">
			<uni-calendar :lunar="true" :insert="true" :date="selectDayBean.date_str" class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="dateChange"  />
		</u-popup>
	</view>
</template>

<script>
	
	export default {

		props: {
			attr: {
				type: Object,
				default: () => {}
			},
			limitNum: {
				type: Number,
				value: 0
			},
			isShow: {
				type: Number,
				value: 0
			},
			iSbnt: {
				type: Number,
				value: 0
			},
			iSplus: {
				type: Number,
				value: 0
			},
			iScart: {
				type: Number,
				value: 0
			},
			isDate: {
				type: Number,
				value: 0
			},
			salesDates: {
				type: Array,
				default: () => []
			},
			info: {
				type: Object,
				default: () => {}
			},
			is_vip: {
				type: Number,
				value: 0
			}
		},
		watch:{
		},
		data() {
			return {
				selectDayBean:{date:'',week:''},
				calendarShow: false,
				flag:0,
				
			};
		},
		mounted() {
		},
		created() {
		},
		methods: {
			/***选择日期**/
			selectDay:function(item){
				let that = this
				that.salesDates.forEach(function(res,index){
					res.selected = false
					if(res.date == item.date){
						res.selected = true
						that.selectDayBean = res
						that.flag++		
					}
				})
				this.$emit('selectedDaysVal', that.salesDates);
				this.$emit('ChangeSelectDay', that.selectDayBean.date_str);
			},
			dateChange:function(date){
				this.selectDayCalendar(date)
				this.calendarShow =false
				
			},
			selectDayCalendar:function(item){
				let that = this
				that.salesDates.forEach(function(res,index){
					that.salesDates[index].selected = false
					if(res.date_str == item.fulldate){
						that.salesDates[index].selected = true
						that.selectDayBean = res		
					}
				})
				this.$emit('selectedDaysVal', that.salesDates);
				this.$emit('ChangeSelectDay', that.selectDayBean.date_str);
			},
			getpreviewImage: function() {
				uni.previewImage({
					urls: this.attr.productSelect.image.split(','),
					current: this.attr.productSelect.image
				});
			},
			selectAttr: function() {
				this.$emit('selectAttr');
			},
			/**
			 * 购物车手动输入数量
			 * 
			 */
			bindCode: function(e) {
				this.$emit('iptCartNum', this.attr.productSelect.cartNum);
			},
			closeAttr: function() {
				this.$emit('myevent');
			},
			CartNumDes: function() {
				this.$emit('ChangeCartNum', false);
			},
			CartNumAdd: function() {
				this.$emit('ChangeCartNum', true);
			},
			tapAttr: function(indexw, indexn) {
				console.log(indexw, indexn)
				let that = this;
				that.$emit("attrVal", {
					indexw: indexw,
					indexn: indexn
				});
				this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attrValues[indexn]);
				let value = that
					.getCheckedValue()
					.join(",");
				that.$emit("ChangeAttr", value);
			},
			//获取被选中属性；
			getCheckedValue: function() {
				let productAttr = this.attr.productAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attrValues.length; j++) {
						if (productAttr[i].index === productAttr[i].attrValues[j]) {
							value.push(productAttr[i].attrValues[j]);
						}
					}
				}
				return value;
			}
		}
	}
</script>

<style scoped lang="scss">
	.vip-money {
		color: #282828;
		font-size: 28rpx;
		font-weight: 700;
		margin-left: 6rpx;
	}

	.vipImg {
		width: 68rpx;
		height: 27rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.product-window {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		background-color: #fff;
		z-index: 1000;
		border-radius: 16rpx 16rpx 0 0;
		transform: translate3d(0, 100%, 0);
		transition: all .3s cubic-bezier(.25, .5, .5, .9);
		// padding-bottom: 140rpx;
		// padding-bottom: calc(140rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		// padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		padding-bottom: 30rpx;
	}

	.product-window.on {
		transform: translate3d(0, 0, 0);
	}

	.product-window.join {
		padding-bottom: 30rpx;
	}

	.product-window.joinCart {
		padding-bottom: 30rpx;
		z-index: 10000;
	}

	.product-window .textpic {
		padding: 0 130rpx 0 30rpx;
		margin-top: 29rpx;
		position: relative;
	}

	.product-window .textpic .pictrue {
		width: 150rpx;
		height: 150rpx;
	}

	.product-window .textpic .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.product-window .textpic .text {
		width: 410rpx;
		font-size: 32rpx;
		color: #202020;
	}

	.product-window .textpic .text .money {
		font-size: 24rpx;
		margin-top: 40rpx;
	}

	.product-window .textpic .text .money .num {
		font-size: 36rpx;
	}

	.product-window .textpic .text .money .stock {
		color: #999;
		margin-left: 6rpx;
	}

	.product-window .textpic .iconfont {
		position: absolute;
		right: 30rpx;
		top: -5rpx;
		font-size: 44rpx;
		color: #8a8a8a;
	}

	.product-window .rollTop {
		max-height: 800rpx;
		overflow: auto;
		margin-top: 36rpx;
	}
    .more_date{
		border: 1px solid #d9d9d9;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 34rpx;
		border-radius: 50rpx;
		margin: 20rpx 0 0 14rpx;
		width: 23%;
		text-align: center;
	}
	.product-window .productWinList .item~.item {
		margin-top: 36rpx;
		margin-bottom: 36rpx;
	}

	.product-window .productWinList .item .title {
		font-size: 30rpx;
		color: #343434;
		padding: 0 30rpx;
	}

	.product-window .productWinList .item .listn {
		padding: 0 30rpx 0 16rpx;
	}

	.product-window .productWinList .item .listn .itemn {
		border: 1px solid #F2F2F2;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 33rpx;
		border-radius: 25rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
	}
	
	.product-window .productWinList .item .listn .itemn2 {
		border: 1px solid #F2F2F2;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 33rpx;
		border-radius: 50rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
		width: 23%;
		text-align: center;
	}
  
	.product-window .productWinList .item .listn .itemn.on {
		@include main_color(theme);
		@include coupons_border_color(theme);
		@include cate-two-btn(theme);
	}


	.product-window .productWinList .item .listn .itemn.limit {
		color: #999;
		text-decoration: line-through;
	}

	.product-window .cart {
		margin-top: 36rpx;
		padding: 0 30rpx;
		padding-bottom: 30px;
	}

	.product-window .cart .title {
		font-size: 30rpx;
		color: #999;
	}

	.product-window .cart .carnum {
		height: 54rpx;
		margin-top: 24rpx;
	}

	.product-window .cart .carnum .iconfont {
		font-size: 42rpx;
	}

	.product-window .cart .carnum view {
		// border: 1px solid #a4a4a4;
		width: 84rpx;
		text-align: center;
		height: 100%;
		line-height: 54rpx;
		color: #282828;
		font-size: 45rpx;
	}

	.product-window .cart .carnum .reduce {
		border-right: 0;
		border-radius: 6rpx 0 0 6rpx;
		line-height: 48rpx;
		font-size: 60rpx;
	}

	.product-window .cart .carnum .reduce.on {
		// border-color: #e3e3e3;
		color: #DEDEDE;
	}

	.product-window .cart .carnum .plus {
		border-left: 0;
		border-radius: 0 6rpx 6rpx 0;
		line-height: 46rpx;
	}

	.product-window .cart .carnum .plus.on {
		// border-color: #e3e3e3;
		color: #dedede;
	}

	.product-window .cart .carnum .num {
		background: rgba(242, 242, 242, 1);
		color: #282828;
		font-size: 28rpx;
	}

	.product-window .joinBnt {
		font-size: 30rpx;
		width: 620rpx;
		height: 86rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 86rpx;
		color: #fff;
		margin: 21rpx auto 0 auto;
	}

	.product-window .joinBnt.on {
		background-color: #bbb;
		color: #fff;
	}
</style>
